﻿namespace Pyramid.Tests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Pyramid.Hardware;
    using Pyramid.Hardware.Interop;

    [TestClass]
    public class CounterTest
    {
        [TestMethod]
        public void ShouldIncrementOnEachClockCycle()
        {
            var resetLine = FixedInputLine.False;

            var clock = new ManualClock();

            var target = new AsynchronousCounter(Tryte.TritsPerTryte, clock.Output, resetLine);

            var outputBus = new OutputBus(target.OutputBus);

            for (int incrementNumber = 0; incrementNumber < Tryte.MaxValue; incrementNumber++)
            {
                clock.Cycle();

                Assert.AreEqual(incrementNumber + 1, (int)outputBus.Value.ToTryte());
            }
        }

        [TestMethod]
        public void ShouldReset()
        {
            var resetLine = new VariableInputLine(Trivalent.False);

            var clock = new ManualClock();

            var target = new AsynchronousCounter(Tryte.TritsPerTryte, clock.Output, resetLine);

            var outputBus = new OutputBus(target.OutputBus);

            for (int incrementNumber = 0; incrementNumber < 10; incrementNumber++)
            {
                clock.Cycle();

                Assert.AreEqual(incrementNumber + 1, (int)outputBus.Value.ToTryte());
            }

            resetLine.SetInput(Trivalent.True);

            clock.Cycle();

            Assert.AreEqual(0, (int)outputBus.Value.ToTryte());
        }
    }
}
